import config
import time
from dateutil import rrule
from datetime import datetime
from utils import *


def register():
	return DateDifference()


class DateDifference:
	def __init__(self):
		self.type = 'passive'
		self.help = \
'''日期差计算：
	用法：
		*日期差 (年-月-日)
		*日期差 (年-月-日) (年-月-日)
	查询今天与指定日期，或两日期之间的日期差。'''

	def run(self, gum, msg):
		if not msg['type'] in ['FriendMessage', 'GroupMessage'] \
				or len(msg['messageChain']) <= 1 \
				or msg['messageChain'][1]['type'] != 'Plain' \
				or msg['messageChain'][1]['text'].split()[0] != '*日期差' \
				or len(msg['messageChain'][1]['text'].split()) <= 1:
			return

		with trying():
			splitted = msg['messageChain'][1]['text'].split()

			date1 = time.strptime(splitted[1], '%Y-%m-%d')
			if len(splitted) > 2:
				date2 = time.strptime(splitted[2], '%Y-%m-%d')
			else:
				date2 = time.strptime(time.strftime('%Y-%m-%d'), '%Y-%m-%d')
			diff = int(abs(time.mktime(date2) - time.mktime(date1)) / 86400)

			chain = MsgChain()
			chain.addPlain(f'日期差为{diff}天呢')
			autoReply(chain, gum)